package com.ldf.jrtsp.parser;

import com.ldf.jrtsp.enums.MediaType;
import com.ldf.jrtsp.enums.RtpPayloadType;
import com.ldf.jrtsp.enums.RtspMediaProtocolType;

import static com.ldf.jrtsp.constants.RtspConstants.RTSP_DELIMITER;

/**
 * sdp解析
 *
 * @author lidaofu
 * @since 2024/6/20
 **/
public class RtspSdpParser {
    private RtpPayloadType videoPayloadType;
    private RtpPayloadType audioPayloadType;
    private RtspMediaProtocolType videoProtocolType;
    private String controlUrl;
    private String videoControlUrl;
    private int videoWidth;
    private int videoHeight;
    private int videoSamplingRate;
    private int videoPort;

    private String audioControlUrl;
    private int audioSamplingRate;
    private int audioChannel;
    private int audioPort;
    private RtspMediaProtocolType audioProtocolType;
    private MediaType[] mediaTypes = new MediaType[2];

    public RtspSdpParser(String sdp) {
        String[] split = sdp.split(RTSP_DELIMITER);
        for (String kv : split) {
            String[] kvSplit = kv.split("=", 2);
            switch (kvSplit[0]) {
                case "a":
                    if (kvSplit[1].startsWith("rtpmap")) {
                        String[] rtpmap = kvSplit[1].substring(7).split(" ");
                        String[] codecAndSamplingAndChannel = rtpmap[1].split("/");
                        String codec = codecAndSamplingAndChannel[0];
                        int samplingRate = Integer.parseInt(codecAndSamplingAndChannel[1]);
                        RtpPayloadType payloadType = RtpPayloadType.getRespPayloadType(codec);
                        if (payloadType.getMediaType()==MediaType.VIDEO) {
                            this.videoPayloadType = payloadType;
                            this.videoSamplingRate = samplingRate;
                        } else {
                            this.audioPayloadType = payloadType;
                            this.audioSamplingRate = samplingRate;
                            if (codecAndSamplingAndChannel.length == 3) {
                                this.audioChannel = Integer.parseInt(codecAndSamplingAndChannel[2]);
                            } else {
                                this.audioChannel = 1;
                            }
                        }
                    } else if (kvSplit[1].startsWith("control")) {
                        String control = kvSplit[1].substring(8);
                        if (control.contains("trackID")) {
                            if (this.videoControlUrl == null) {
                                this.videoControlUrl = control;
                            } else {
                                this.audioControlUrl = control;
                            }
                        } else {
                            this.controlUrl = control;
                        }
                    } else if (kvSplit[1].startsWith("x-dimensions")) {
                        if (this.videoWidth == 0) {
                            String[] wh = kvSplit[1].substring(13).split(",");
                            this.videoWidth = Integer.parseInt(wh[0]);
                            this.videoHeight = Integer.parseInt(wh[1]);
                        }
                    }
                    break;
                case "b":
                    // 带宽
                        /*if (kvSplit[1].startsWith("AS")) {

                        }*/
                    break;
                case "m":
                    if (kvSplit[1].startsWith(MediaType.VIDEO.getValue())) {
                        String[] m = kvSplit[1].substring(6).split(" ");
                        this.videoPort = Integer.parseInt(m[0]);
                        this.videoProtocolType = RtspMediaProtocolType.getRespMediaProtocolType(m[1]);
                        mediaTypes[0] = MediaType.VIDEO;
                    } else if (kvSplit[1].startsWith((MediaType.AUDIO.getValue()))) {
                        String[] m = kvSplit[1].substring(6).split(" ");
                        this.audioChannel = Integer.parseInt(m[0]);
                        this.audioProtocolType = RtspMediaProtocolType.getRespMediaProtocolType(m[1]);
                        mediaTypes[1] = MediaType.AUDIO;
                    }
                    break;
            }
        }
    }

    public RtpPayloadType getVideoPayloadType() {
        return videoPayloadType;
    }

    public void setVideoPayloadType(RtpPayloadType videoPayloadType) {
        this.videoPayloadType = videoPayloadType;
    }

    public String getVideoControlUrl() {
        return videoControlUrl;
    }

    public void setVideoControlUrl(String videoControlUrl) {
        this.videoControlUrl = videoControlUrl;
    }

    public int getVideoWidth() {
        return videoWidth;
    }

    public void setVideoWidth(int videoWidth) {
        this.videoWidth = videoWidth;
    }

    public int getVideoHeight() {
        return videoHeight;
    }

    public void setVideoHeight(int videoHeight) {
        this.videoHeight = videoHeight;
    }

    public int getVideoSamplingRate() {
        return videoSamplingRate;
    }

    public void setVideoSamplingRate(int videoSamplingRate) {
        this.videoSamplingRate = videoSamplingRate;
    }

    public RtpPayloadType getAudioPayloadType() {
        return audioPayloadType;
    }

    public void setAudioPayloadType(RtpPayloadType audioPayloadType) {
        this.audioPayloadType = audioPayloadType;
    }

    public String getAudioControlUrl() {
        return audioControlUrl;
    }

    public void setAudioControlUrl(String audioControlUrl) {
        this.audioControlUrl = audioControlUrl;
    }

    public int getAudioSamplingRate() {
        return audioSamplingRate;
    }

    public void setAudioSamplingRate(int audioSamplingRate) {
        this.audioSamplingRate = audioSamplingRate;
    }

    public int getAudioChannel() {
        return audioChannel;
    }

    public void setAudioChannel(int audioChannel) {
        this.audioChannel = audioChannel;
    }

    public int getVideoPort() {
        return videoPort;
    }

    public void setVideoPort(int videoPort) {
        this.videoPort = videoPort;
    }

    public RtspMediaProtocolType getVideoProtocolType() {
        return videoProtocolType;
    }

    public void setVideoProtocolType(RtspMediaProtocolType videoProtocolType) {
        this.videoProtocolType = videoProtocolType;
    }

    public int getAudioPort() {
        return audioPort;
    }

    public void setAudioPort(int audioPort) {
        this.audioPort = audioPort;
    }

    public RtspMediaProtocolType getAudioProtocolType() {
        return audioProtocolType;
    }

    public void setAudioProtocolType(RtspMediaProtocolType audioProtocolType) {
        this.audioProtocolType = audioProtocolType;
    }

    public MediaType[] getMediaTypes() {
        return mediaTypes;
    }

    public void setMediaTypes(MediaType[] mediaTypes) {
        this.mediaTypes = mediaTypes;
    }

    public String getControlUrl() {
        return controlUrl;
    }

    public void setControlUrl(String controlUrl) {
        this.controlUrl = controlUrl;
    }
}
